__author__ = 'dl153'
# -*- coding:utf-8 -*-

import cx_Oracle
import pandas as pd
# 执行插入语句
def sql_conn():
    sql_user = "sjz_dm"
    sql_pwd = "sjz_dm2023"
    sql_db = "dataming"
    global conn ,cursor
    conn  = cx_Oracle.connect(sql_user + '/' + sql_pwd + '@' + sql_db)
    cursor = conn .cursor()

# 执行单挑SQL语句(不返回结果)
def exe(str_sql):
    sql_conn()
    cursor.execute(str_sql)
    conn.commit()
    cursor.close()
    conn.close()

# 执行批量SQL语句(不返回结果)
def exe_many(str_sql, list_line):
    # print str_sql
    sql_conn()
    cursor.executemany(str_sql, list_line)
    conn.commit()
    cursor.close()
    conn.close()

# 执行查询语句(返回查询结果)
def select_df(str_sql):
    sql_conn()
    df = pd.read_sql(str_sql,conn)
    conn.close()

    return df

# 执行查询语句(返回查询结果)
def select_with_header(str_sql):
    sql_conn()
    cursor.execute(str_sql)
    results = cursor.fetchall()
# 获取字段  即多少列
    columns = [desc[0] for desc in cursor.description]
    results_with_header = [columns]+results
    conn.close()
    return results_with_header
